package cn.com.lsy.product.lepao.service.impl;

import cn.com.lsy.product.lepao.model.dao.LpTrainTypeVideoTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpTrainVideoChildTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpTrainVideoLabelTblMapper;
import cn.com.lsy.product.lepao.model.entities.*;
import cn.com.lsy.product.lepao.model.mt.dao.MtTrainTypeVideoMapper;
import cn.com.lsy.product.lepao.model.mt.entities.TrainTypeVideoListDto;
import cn.com.lsy.product.lepao.service.ITrainCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TrainCenterService implements ITrainCenterService {

    @Autowired
    MtTrainTypeVideoMapper mtTrainTypeVideoMapper;
    @Autowired
    LpTrainTypeVideoTblMapper lpTrainTypeVideoTblMapper;
    @Autowired
    LpTrainVideoLabelTblMapper lpTrainVideoLabelTblMapper;
    @Autowired
    LpTrainVideoChildTblMapper lpTrainVideoChildTblMapper;


    @Override
    public List<TrainTypeVideoListDto> selectActionLibraryByPage(int start, int end, String searchCondition, String type) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("start", start);
        where.put("end", end);
        where.put("searchCondition", searchCondition);
        where.put("type", type);

        return mtTrainTypeVideoMapper.selectByPage(where);
    }

    @Override
    public int selectActionLibraryByCount(String searchCondition, String type) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("searchCondition", searchCondition);
        where.put("type", type);

        return mtTrainTypeVideoMapper.selectByCount(where);
    }

    @Override
    public LpTrainTypeVideoTbl selectActionLibraryInfo(int id) {

        return lpTrainTypeVideoTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateActionLibrary(LpTrainTypeVideoTbl param) {

        if (lpTrainTypeVideoTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean insertActionLibrary(LpTrainTypeVideoTbl param) {

        if (lpTrainTypeVideoTblMapper.insertSelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public List<LpTrainVideoLabelTbl> selectTrainVideoLabel() {

        LpTrainVideoLabelTblExample example = new LpTrainVideoLabelTblExample();

        return lpTrainVideoLabelTblMapper.selectByExample(example);
    }

    @Override
    public LpTrainVideoLabelTbl selectTrainVideoLabelInfo(int id) {

        return lpTrainVideoLabelTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateTrainVideoLabel(LpTrainVideoLabelTbl param) {

        if (lpTrainVideoLabelTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean insertTrainVideoLabel(LpTrainVideoLabelTbl param) {

        //查询名称是否存在
        LpTrainVideoLabelTblExample example = new LpTrainVideoLabelTblExample();
        example.createCriteria().andNameEqualTo(param.getName());

        if (lpTrainVideoLabelTblMapper.countByExample(example) <= 0) {

            if (lpTrainVideoLabelTblMapper.insertSelective(param) > 0) {

                return true;

            } else {

                return false;
            }

        } else {

            return false;
        }

    }

    @Override
    public boolean delTrainVideoLabel(int id) {

        if (lpTrainVideoLabelTblMapper.deleteByPrimaryKey(id) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public LpTrainVideoChildTbl selectActionLibraryChildInfo(int parentId) {

        LpTrainVideoChildTblExample example = new LpTrainVideoChildTblExample();
        example.createCriteria().andParentIdEqualTo(parentId);

        List<LpTrainVideoChildTbl> list = lpTrainVideoChildTblMapper.selectByExample(example);

        if (list != null && list.size() > 0) {

            return list.get(0);

        } else {

            return null;
        }

    }

    @Override
    public boolean insertActionLibraryChild(LpTrainVideoChildTbl param) {

        if (lpTrainVideoChildTblMapper.insertSelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean updateActionLibraryChild(LpTrainVideoChildTbl param) {

        if (lpTrainVideoChildTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean delActionLibraryChild(int id) {

        LpTrainVideoChildTblExample example = new LpTrainVideoChildTblExample();
        example.createCriteria().andParentIdEqualTo(id);

        if (lpTrainVideoChildTblMapper.deleteByExample(example) > 0) {

            return true;

        } else {

            return false;
        }
    }
}
